package cn.iocoder.yudao.module.handguard.controller.admin.productaccessrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.admin.productaccessrecord.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.productaccessrecord.ProductAccessRecordDO;
import cn.iocoder.yudao.module.handguard.convert.productaccessrecord.ProductAccessRecordConvert;
import cn.iocoder.yudao.module.handguard.service.productaccessrecord.ProductAccessRecordService;

@Tag(name = "管理后台 - 产品准入-产品备注/产品特点记录")
@RestController
@RequestMapping("/hutai/product-access-record")
@Validated
public class ProductAccessRecordController {

    @Resource
    private ProductAccessRecordService productAccessRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建产品准入-产品备注/产品特点记录")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-record:create')")
    public CommonResult<Long> createProductAccessRecord(@Valid @RequestBody ProductAccessRecordCreateReqVO createReqVO) {
        return success(productAccessRecordService.createProductAccessRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品准入-产品备注/产品特点记录")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-record:update')")
    public CommonResult<Boolean> updateProductAccessRecord(@Valid @RequestBody ProductAccessRecordUpdateReqVO updateReqVO) {
        productAccessRecordService.updateProductAccessRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品准入-产品备注/产品特点记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:product-access-record:delete')")
    public CommonResult<Boolean> deleteProductAccessRecord(@RequestParam("id") Long id) {
        productAccessRecordService.deleteProductAccessRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品准入-产品备注/产品特点记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-record:query')")
    public CommonResult<ProductAccessRecordRespVO> getProductAccessRecord(@RequestParam("id") Long id) {
        ProductAccessRecordDO productAccessRecord = productAccessRecordService.getProductAccessRecord(id);
        return success(ProductAccessRecordConvert.INSTANCE.convert(productAccessRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品准入-产品备注/产品特点记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-record:query')")
    public CommonResult<List<ProductAccessRecordRespVO>> getProductAccessRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<ProductAccessRecordDO> list = productAccessRecordService.getProductAccessRecordList(ids);
        return success(ProductAccessRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品准入-产品备注/产品特点记录分页")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-record:query')")
    public CommonResult<PageResult<ProductAccessRecordRespVO>> getProductAccessRecordPage(@Valid ProductAccessRecordPageReqVO pageVO) {
        PageResult<ProductAccessRecordDO> pageResult = productAccessRecordService.getProductAccessRecordPage(pageVO);
        return success(ProductAccessRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品准入-产品备注/产品特点记录 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-record:export')")
    @OperateLog(type = EXPORT)
    public void exportProductAccessRecordExcel(@Valid ProductAccessRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProductAccessRecordDO> list = productAccessRecordService.getProductAccessRecordList(exportReqVO);
        // 导出 Excel
        List<ProductAccessRecordExcelVO> datas = ProductAccessRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "产品准入-产品备注/产品特点记录.xls", "数据", ProductAccessRecordExcelVO.class, datas);
    }

}
